/******************************************************************************
 *      Mestrado em Ciência da Computação - UFMS
 *		Redes de Computadores - Prof. Dr. Ronaldo Alves Ferreira
 *		Alunos: Geraldo Landre, Marcel Tolentino, Vinícius Bueno
 *****************************************************************************/
/* tcpclosing.c - tcpclosing */

#include "tcpfms.h"
#include "tcb.h"

/*------------------------------------------------------------------------
 *  tcpclosing -  do CLOSING state input processing
 *------------------------------------------------------------------------
 */
int tcpclosing(struct tcb *ptcb, struct udp *pudp)
{
	//struct	ip	*pip = (struct ip *)pep->ep_data;
	struct	tcp	*ptcp = (struct tcp*)(pudp->udp_data);

	if (ptcp->tcp_code & TCPF_RST)
		return tcbdealloc(ptcb);
	if (ptcp->tcp_code & TCPF_SYN) {
		tcpreset(pudp);
		return tcbdealloc(ptcb);
	}
	tcpacked(ptcb, pudp);
	if ((ptcb->tcb_code & TCPF_FIN) == 0) {
		ptcb->tcb_state = TCPS_TIMEWAIT;
		sem_post(&(ptcb->tcb_ocsem));		/* wake closer	*/
		tcpwait((struct tcb *)ptcb);
	}
	return OK;
}
